package com.css.fxfzysx.modules.lifeline.service.impl;

import com.css.fxfzysx.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzysx.base.attachment.service.AttachmentInfoService;
import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.constants.YNEnum;
import com.css.fxfzysx.modules.cztDataMange.exception.ShpFieldException;
import com.css.fxfzysx.modules.cztDataMange.result.ParsingResult;
import com.css.fxfzysx.modules.lifeline.constants.TableNameConstants;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.*;
import com.css.fxfzysx.modules.lifeline.param.AnalysisParam;
import com.css.fxfzysx.modules.lifeline.param.CircleParam;
import com.css.fxfzysx.modules.lifeline.param.PageParam;
import com.css.fxfzysx.modules.lifeline.param.RegionParam;
import com.css.fxfzysx.modules.lifeline.repository.LifelineEngineeringRepository;
import com.css.fxfzysx.modules.lifeline.rest.JcsjRestManager;
import com.css.fxfzysx.modules.lifeline.service.LifelineEngineeringService;
import com.css.fxfzysx.modules.lifeline.utils.ShpUtils;
import com.css.fxfzysx.modules.lifeline.utils.VersionCodeUtils;
import com.css.fxfzysx.modules.lifeline.vo.LifelineIdVo;
import com.css.fxfzysx.modules.lifeline.vo.SmxVersionVo;
import com.css.fxfzysx.modules.lifeline.vo.fxqh.DataSourceVo;
import com.css.fxfzysx.modules.lifeline.vo.fxqh.EvaluationResults;
import com.css.fxfzysx.modules.vaolEngineering.service.*;
import com.css.fxfzysx.modules.vaolEngineering.utils.MainTableUtils;
import com.css.fxfzysx.util.*;
import com.css.fxfzysx.util.hypergraph.service.HypergraphService;
import com.css.fxfzysx.web.PlatformSessionContext;
import com.css.fxfzysx.zcpt.sys.entity.SUser;
import com.css.fxfzysx.zcpt.sys.service.SUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class LifelineEngineeringServiceImpl implements LifelineEngineeringService {

    @Autowired
    private LifelineEngineeringRepository lifelineEngineeringRepository;

    @Autowired
    private AttachmentInfoService attachmentInfoService;

    @Autowired
    private VersionCodeUtils versionCodeUtils;

    @Autowired
    private SUserService userService;

    @Autowired
    private MainTableUtils mainTableUtils;

    @Autowired
    private CarVulnerabilityService carVulnerabilityService;  //道路

    @Autowired
    private UpNetworkService upNetworkService; //地下管网

    @Autowired
    private CbVulnerabilityService cbVulnerabilityService; //桥梁

    @Autowired
    private WsfManagementService wsfManagementService; // 供水

    @Autowired
    private SgManagementService sgManagementService; //供气

    @Autowired
    private CeVulnerabilityService ceVulnerabilityService;

    @Autowired
    private TsVulnerabilityService tsVulnerabilityService;

    @Autowired
    private ShpUtils shpUtils;

    @Autowired
    private HypergraphService hypergraphService;


    @Override
    public List<SmxVersionEntity> getRepeatData(RegionParam param) {
        param.setUserId(PlatformSessionContext.getUserID());
        if (PlatformObjectUtils.isEmpty(param.getName())) {
            String fileName = attachmentInfoService.getAttach(param.getFileIds().split(",")[0]).getAttachName();
            param.setName(fileName.split("\\.")[0]);
        }
        List<SmxVersionEntity> list = lifelineEngineeringRepository.getByRegionAndUserAndType(param, param.getName());
        list.forEach(it -> {
            if (PlatformObjectUtils.isNotEmpty(it.getUpdateTime())) {
                it.setCreateTime(it.getUpdateTime());
            }
        });
        return list;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> uploadShp(RegionParam param) {
        Integer type = param.getType();
        String fileIds = param.getFileIds();
        String versionId = "";
        //保存版本信息
        if (param.getCover() == 1) {
            versionId = updateVersionInfo(param);
            //删除主表数据
            lifelineEngineeringRepository.deleteMainByVersionId(versionId);
            //删除历史的数据
            lifelineEngineeringRepository.deleteOldVersionData(versionId, type, param.getBridgeType());
        } else {
            versionId = saveVersionInfo(param);
        }

        Map<String, Object> map = new HashMap<>();
        try {
            String userId = PlatformSessionContext.getUserID();
            if (PlatformObjectUtils.isNotEmpty(param.getUserId())) {
                userId = param.getUserId();
            }
            if (type == 1) {
                //地下管网
                map = upNetworkService.uploadUpNetwork(fileIds, versionId, userId);
            } else if (type == 2) {
                //水厂水池
                map = wsfManagementService.uploadWaterSupply(fileIds, versionId, userId);
            } else if (type == 3) {
                //储气罐
                map = sgManagementService.uploadAirReceiver(fileIds, versionId, userId);
            } else if (type == 4) {
                //桥梁
                map = cbVulnerabilityService.uploadBridge(fileIds, versionId, userId, param.getBridgeType());
            } else if (type == 5) {
                //道路
                map = carVulnerabilityService.uploadRoadShp(fileIds, versionId, userId);
            } else if (type == 6) {
                //变电站
                map = tsVulnerabilityService.uploadTsVulnerability(fileIds, versionId, userId);
            } else if (type == 7) {
                //架空线路
                map = uploadOverheadLineShp(fileIds, versionId, userId);
            } else if (type == 8) {
                //电缆线路
                map = uploadCableRouteShp(fileIds, versionId, userId);
            } else if (type == 9) {
                //通信室内设备
                map = ceVulnerabilityService.uploadDevices(fileIds, versionId, userId);
            } else if (type == 10) {
                //电视塔
                map = uploadTvTowerShp(fileIds, versionId, userId);
            }
            if(type == 4){
                if(param.getBridgeType() == 1){
                    type = 44;
                }
            }
            hypergraphService.getWorkspaceReload(TableNameConstants.getMainConstant(type));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            map.put("res", false);
            map.put("mess", "该数据类别下没有shp数据");
        }
        return map;
    }

    public String saveVersionInfo(RegionParam param) {
        SmxVersionEntity entity = new SmxVersionEntity();
        entity.setId(UUIDGenerator.getUUID());
        entity.setVersionCode(versionCodeUtils.getVersionCode(param.getType()));
        entity.setProvince(param.getProvince());
        entity.setCity(param.getCity());
        entity.setCounty(param.getCounty());
        entity.setFileIds(param.getFileIds());
        AttachmentInfoEntity attach = attachmentInfoService.getAttach(param.getFileIds().split(",")[0]);
        String name = attach.getAttachName().split("\\.")[0];
        entity.setFileName(name);
        entity.setVersionStatus(1);
        entity.setCreateUser(PlatformSessionContext.getUserID());
        entity.setCreateUserName(getUserName().getUserName());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        entity.setDelFlag("0");
        entity.setType(param.getType());
        entity.setBridgeType(param.getBridgeType());
        lifelineEngineeringRepository.saveSmxVersion(entity);
        return entity.getId();
    }

    public String updateVersionInfo(RegionParam param) {
        SmxVersionEntity entity = new SmxVersionEntity();
        entity.setFileIds(param.getFileIds());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        entity.setUpdateUser(PlatformSessionContext.getUserID());
        entity.setId(param.getId());
        lifelineEngineeringRepository.updateSmxVersion(entity);
        return entity.getId();
    }

    /**
     * 获取用户名字
     *
     * @return
     */
    public SUser getUserName() {
        String user = userService.getSUser(PlatformSessionContext.getUserID());
        return CreateGson.createGson().fromJson(user, SUser.class);
    }

    @Override
    public Map<String, Object> getVersionCode(RegionParam param) {
        Map<String, Object> data = lifelineEngineeringRepository.getVersionCode(param);
        data.forEach((k, v) -> {
            if (k.equals("rows")) {
                if (PlatformObjectUtils.isNotEmpty(v)) {
                    ArrayList<SmxVersionVo> voList = new ArrayList<>();
                    List<SmxVersionEntity> list = (List<SmxVersionEntity>) v;
                    list.forEach(it -> {
                        SmxVersionVo vo = new SmxVersionVo();
                        if (PlatformObjectUtils.isNotEmpty(it.getUpdateTime())) {
                            it.setCreateTime(it.getUpdateTime());
                        }
                        BeanUtils.copyProperties(it, vo);
                        vo.setRegion(it.getProvince() + "-" + it.getCity() + "-" + it.getCounty());
                        voList.add(vo);
                    });
                    data.put("rows", voList);
                }
            }
        });
        return data;
    }

    @Override
    public Map<String, Object> getLifelineDataSource(PageParam param) {
        Map<String, Object> map = lifelineEngineeringRepository.getLifelineDataSource(param);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.equals("rows")) {
                List<DataSourceVo> list = (List<DataSourceVo>) entry.getValue();
                for (DataSourceVo entity : list) {
                    //查询是否全部未计算
                    Integer status = lifelineEngineeringRepository.getAllCalculationResultsByVersionId(entity.getId());
                    entity.setCount(2);
                    if (status == 0) {
                        entity.setCount(0);
                    } else if (lifelineEngineeringRepository.getCalculationResultsByVersionId(entity.getId()) != 0) {
                        entity.setCount(1);
                    }
                    if (param.getType() == 3 || param.getType() == 6 || param.getType() == 9) {
                        entity.setCount(0);
                    }
                }
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> getByTypeAndVersionId(int page, int size, String versionId, Integer type) {
        return lifelineEngineeringRepository.getByTypeAndVersionId(page, size, versionId, type);
    }

    @Override
    public List<EvaluationResults> getAnalysisByVersionId(AnalysisParam param) {
        String versionId = param.getVersionId();
        return lifelineEngineeringRepository.getAnalysisByVersionId(param.getType(), versionId);
    }

    @Override
    public void deleteByTypeAndId(Integer type, String ids) {
        String id = spilt(ids);
        for (String dataId : ids.split(",")) {
            LifelineIdVo lifelineId = lifelineEngineeringRepository.getTableIdByIdAndType(dataId, type);
            if (type < 15) {
                lifelineEngineeringRepository.deleteMainByTableId(lifelineId.getTableId());
                if (type > 10) {
                    lifelineEngineeringRepository.deleteGhByTableId(lifelineId.getTableId());
                    lifelineEngineeringRepository.deleteZhByTableId(lifelineId.getTableId());
                }
            }
            int count = lifelineEngineeringRepository.getDataCountByVersionId(type, lifelineId.getVersionId());
            if (type == 4) {
                count += lifelineEngineeringRepository.getBridgeLineCountByVersionId(lifelineId.getVersionId());
            }
            if (count == 1) {
                lifelineEngineeringRepository.deleteByTypeAndVersionId(lifelineId.getVersionId());
            }
        }
        lifelineEngineeringRepository.deleteByTypeAndId(id, type);
        if (type == 4) {
            lifelineEngineeringRepository.deleteLineById(id);
        }
    }

    public static String spilt(String str) {
        StringBuffer sb = new StringBuffer();
        String[] temp = str.split(",");
        for (int i = 0; i < temp.length; i++) {
            if (!"".equals(temp[i]) && temp[i] != null)
                sb.append("'" + temp[i] + "',");
        }
        String result = sb.toString();
        String tp = result.substring(result.length() - 1, result.length());
        if (",".equals(tp)) {
            return result.substring(0, result.length() - 1);
        } else {
            return result;
        }
    }


    @Override
    public void updateById(HashMap<Integer, Object> map) {
        for (Map.Entry<Integer, Object> entry : map.entrySet()) {
            String userID = PlatformSessionContext.getUserID();
            Map dataMap = (Map) entry.getValue();
            if (entry.getKey() < 11) {
                String tableId = dataMap.get("tableId").toString();
                mainTableUtils.updateVersionCalculationStatus(dataMap.get("versionId").toString(), 1);
                lifelineEngineeringRepository.updateMainCalculationStatusByTableId(tableId);
            }
            lifelineEngineeringRepository.updateById(entry.getKey(), entry.getValue(), userID);
            if (entry.getKey() == 4) {
                lifelineEngineeringRepository.updateBridgeLineById(entry.getValue(), userID);
            }
        }
    }

    @Override
    public void deleteByTypeAndVersionId(String versionId) {
        SmxVersionEntity entity = lifelineEngineeringRepository.getById(versionId);
        lifelineEngineeringRepository.deleteByTypeAndVersionId(versionId);
        lifelineEngineeringRepository.deleteValueByTypeAndVersionId(versionId, entity.getType(), entity.getBridgeType());
        lifelineEngineeringRepository.deleteMainByVersionId(versionId);
        if (entity.getType() > 10) {
            lifelineEngineeringRepository.deleteByVersionId(versionId);
            lifelineEngineeringRepository.deleteZhByVersionId(versionId);
        }
    }

    private Map<String, Object> uploadOverheadLineShp(String fileIds, String versionId, String userId) throws ShpFieldException {
        Map<String, Object> map = new HashMap<>();
        shpUtils.uniformFileName(fileIds);
        ParsingResult shpData = shpUtils.getShpData(fileIds);
        //是否成功
        if (!shpData.getMessage().equals("ok")) {
            map.put("res", false);
            map.put("mess", shpData.getMessage());
        }
        List<List<Object>> shpList = shpData.getShpList();
        List<SmxOverheadLineEntity> arrayList = new ArrayList<>();
        for (int i = 0; i < shpList.size(); i++) {
            String id = UUIDGenerator.getUUID();
            List<Object> list = shpList.get(i);
            if (list.size() < 51) {
                return null;
            }
            SmxOverheadLineEntity overheadLineEntity = new SmxOverheadLineEntity();
            overheadLineEntity.setDelFlag(YNEnum.N.toString());
            overheadLineEntity.setColId(UUIDGenerator.getUUID());
            overheadLineEntity.setTableId(id);
            overheadLineEntity.setCreateUser(userId);
            overheadLineEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());

            if (!PlatformObjectUtils.isEmpty(list.get(0))) {
                overheadLineEntity.setGeom(list.get(0).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(1))) {
                overheadLineEntity.setId(ShpAnalyticUtil.getStringVal(list.get(1).toString(), "评价单元编码", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(2))) {
                overheadLineEntity.setColName(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "评价单元名称", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(3))) {
                overheadLineEntity.setFortificationIntensity(ShpAnalyticUtil.getStringVal(list.get(3).toString(), "评价单元设防烈度", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(4))) {
                overheadLineEntity.setSite(ShpAnalyticUtil.getStringVal(list.get(4).toString(), "场地类别", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(5))) {
                overheadLineEntity.setVoltageGrade35(ShpAnalyticUtil.getDoubleVal(list.get(5).toString(), "", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(6))) {
                overheadLineEntity.setLineLength35(ShpAnalyticUtil.getDoubleVal(list.get(6).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(7))) {
                overheadLineEntity.setLoopLength35(ShpAnalyticUtil.getDoubleVal(list.get(7).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(8))) {
                overheadLineEntity.setLandform35(ShpAnalyticUtil.getStringVal(list.get(8).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(9))) {
                overheadLineEntity.setUnitCostLine35(ShpAnalyticUtil.getDoubleVal(list.get(9).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(10))) {
                overheadLineEntity.setType35(ShpAnalyticUtil.getStringVal(list.get(10).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(11))) {
                overheadLineEntity.setNumber35(ShpAnalyticUtil.getDoubleVal(list.get(11).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(12))) {
                overheadLineEntity.setUnitCostTower35(ShpAnalyticUtil.getDoubleVal(list.get(12).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(13))) {
                overheadLineEntity.setVoltageGrade66(ShpAnalyticUtil.getDoubleVal(list.get(13).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(14))) {
                overheadLineEntity.setLineLength66(ShpAnalyticUtil.getDoubleVal(list.get(14).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(15))) {
                overheadLineEntity.setLoopLength66(ShpAnalyticUtil.getDoubleVal(list.get(15).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(16))) {
                overheadLineEntity.setLandform66(ShpAnalyticUtil.getStringVal(list.get(16).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(17))) {
                overheadLineEntity.setUnitCostLine66(ShpAnalyticUtil.getDoubleVal(list.get(17).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(18))) {
                overheadLineEntity.setType66(ShpAnalyticUtil.getStringVal(list.get(18).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(19))) {
                overheadLineEntity.setNumber66(ShpAnalyticUtil.getDoubleVal(list.get(19), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(20))) {
                overheadLineEntity.setUnitCostTower66(ShpAnalyticUtil.getDoubleVal(list.get(20).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(21))) {
                overheadLineEntity.setVoltageGrade110(ShpAnalyticUtil.getDoubleVal(list.get(21).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(22))) {
                overheadLineEntity.setLineLength110(ShpAnalyticUtil.getDoubleVal(list.get(22).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(23))) {
                overheadLineEntity.setLoopLength110(ShpAnalyticUtil.getDoubleVal(list.get(23).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(24))) {
                overheadLineEntity.setLandform110(ShpAnalyticUtil.getStringVal(list.get(24).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(25))) {
                overheadLineEntity.setUnitCostLine110(ShpAnalyticUtil.getDoubleVal(list.get(25).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(26))) {
                overheadLineEntity.setType110(ShpAnalyticUtil.getStringVal(list.get(26).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(27))) {
                overheadLineEntity.setNumber110(ShpAnalyticUtil.getDoubleVal(list.get(27), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(28))) {
                overheadLineEntity.setUnitCostTower110(ShpAnalyticUtil.getDoubleVal(list.get(28).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(29))) {
                overheadLineEntity.setVoltageGrade220(ShpAnalyticUtil.getDoubleVal(list.get(29).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(30))) {
                overheadLineEntity.setLineLength220(ShpAnalyticUtil.getDoubleVal(list.get(30).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(31))) {
                overheadLineEntity.setLoopLength220(ShpAnalyticUtil.getDoubleVal(list.get(31).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(32))) {
                overheadLineEntity.setLandform220(ShpAnalyticUtil.getStringVal(list.get(32).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(33))) {
                overheadLineEntity.setUnitCostLine220(ShpAnalyticUtil.getDoubleVal(list.get(33).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(34))) {
                overheadLineEntity.setType220(ShpAnalyticUtil.getStringVal(list.get(34).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(35))) {
                overheadLineEntity.setNumber220(ShpAnalyticUtil.getDoubleVal(list.get(35), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(36))) {
                overheadLineEntity.setUnitCostTower220(ShpAnalyticUtil.getDoubleVal(list.get(36).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(37))) {
                overheadLineEntity.setVoltageGrade330(ShpAnalyticUtil.getDoubleVal(list.get(37).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(38))) {
                overheadLineEntity.setLineLength330(ShpAnalyticUtil.getDoubleVal(list.get(38).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(39))) {
                overheadLineEntity.setLoopLength330(ShpAnalyticUtil.getDoubleVal(list.get(39).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(40))) {
                overheadLineEntity.setLandform330(ShpAnalyticUtil.getStringVal(list.get(40).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(41))) {
                overheadLineEntity.setUnitCostLine330(ShpAnalyticUtil.getDoubleVal(list.get(41).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(42))) {
                overheadLineEntity.setType330(ShpAnalyticUtil.getStringVal(list.get(42).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(43))) {
                overheadLineEntity.setNumber330(ShpAnalyticUtil.getDoubleVal(list.get(43), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(44))) {
                overheadLineEntity.setUnitCostTower330(ShpAnalyticUtil.getDoubleVal(list.get(44).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(45))) {
                overheadLineEntity.setVoltageGrade500(ShpAnalyticUtil.getDoubleVal(list.get(45).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(46))) {
                overheadLineEntity.setLineLength500(ShpAnalyticUtil.getDoubleVal(list.get(46).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(47))) {
                overheadLineEntity.setLoopLength500(ShpAnalyticUtil.getDoubleVal(list.get(47).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(48))) {
                overheadLineEntity.setLandform500(ShpAnalyticUtil.getStringVal(list.get(48).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(49))) {
                overheadLineEntity.setUnitCostLine500(ShpAnalyticUtil.getDoubleVal(list.get(49).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(50))) {
                overheadLineEntity.setType500(ShpAnalyticUtil.getStringVal(list.get(50).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(51))) {
                overheadLineEntity.setNumber500(ShpAnalyticUtil.getDoubleVal(list.get(51), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(52))) {
                overheadLineEntity.setUnitCostTower500(ShpAnalyticUtil.getDoubleVal(list.get(52).toString(), "", false));
            }
            if (list.size() > 53 && !PlatformObjectUtils.isEmpty(list.get(53))) {
                overheadLineEntity.setExtends1(list.get(53).toString());
            }
            if (list.size() > 54 && !PlatformObjectUtils.isEmpty(list.get(54))) {
                overheadLineEntity.setExtends2(list.get(54).toString());
            }
            if (list.size() > 55 && !PlatformObjectUtils.isEmpty(list.get(55))) {
                overheadLineEntity.setExtends3(list.get(55).toString());
            }
            if (list.size() > 56 && !PlatformObjectUtils.isEmpty(list.get(56))) {
                overheadLineEntity.setExtends4(list.get(56).toString());
            }
            if (list.size() > 57 && !PlatformObjectUtils.isEmpty(list.get(57))) {
                overheadLineEntity.setExtends5(list.get(57).toString());
            }
            if (list.size() > 58 && !PlatformObjectUtils.isEmpty(list.get(58))) {
                overheadLineEntity.setExtends6(list.get(58).toString());
            }
            if (list.size() > 59 && !PlatformObjectUtils.isEmpty(list.get(59))) {
                overheadLineEntity.setExtends7(list.get(59).toString());
            }
            if (list.size() > 60 && !PlatformObjectUtils.isEmpty(list.get(60))) {
                overheadLineEntity.setExtends8(list.get(60).toString());
            }
            if (list.size() > 61 && !PlatformObjectUtils.isEmpty(list.get(61))) {
                overheadLineEntity.setExtends9(list.get(61).toString());
            }
            if (list.size() > 62 && !PlatformObjectUtils.isEmpty(list.get(62))) {
                overheadLineEntity.setExtends10(list.get(62).toString());
            }
            overheadLineEntity.setVersionId(versionId);
            arrayList.add(overheadLineEntity);

            SmxMainTableEntity mainEntity = new SmxMainTableEntity();
            mainEntity.setId(id);
            mainEntity.setCreateUser(userId);
            mainEntity.setFortification(overheadLineEntity.getFortificationIntensity());
            mainEntity.setBusinessObject(FxfzConstants.CZT_SMX_JKXL);
            mainEntity.setVersionId(versionId);
            mainTableUtils.saveMainTable(mainEntity);
        }
        boolean res = false;
        int i = 1;
        for (SmxOverheadLineEntity overheadLineEntity : arrayList) {
            overheadLineEntity.setSerialNumber(i);
            res = lifelineEngineeringRepository.saveOverheadLine(overheadLineEntity);
            i += 1;
        }
        map.put("res", res);
        return map;
    }

    private Map<String, Object> uploadCableRouteShp(String fileIds, String versionId, String userId) throws ShpFieldException {
        Map<String, Object> map = new HashMap<>();
        shpUtils.uniformFileName(fileIds);
        ParsingResult shpData = shpUtils.getShpData(fileIds);
        //是否成功
        if (!shpData.getMessage().equals("ok")) {
            map.put("res", false);
            map.put("mess", shpData.getMessage());
        }
        List<List<Object>> shpList = shpData.getShpList();
        List<SmxCableRouteEntity> arrayList = new ArrayList<>();
        for (int i = 0; i < shpList.size(); i++) {
            List<Object> list = shpList.get(i);
            String id = UUIDGenerator.getUUID();
            if (list.size() < 8) {
                return null;
            }
            SmxCableRouteEntity cableRouteEntity = new SmxCableRouteEntity();
            cableRouteEntity.setDelFlag(YNEnum.N.toString());
            cableRouteEntity.setCcrId(UUIDGenerator.getUUID());
            cableRouteEntity.setTableId(id);
            cableRouteEntity.setCreateUser(userId);
            cableRouteEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());

            if (!PlatformObjectUtils.isEmpty(list.get(0))) {
                cableRouteEntity.setGeom(list.get(0).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(1))) {
                cableRouteEntity.setId(ShpAnalyticUtil.getStringVal(list.get(1).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(2))) {
                cableRouteEntity.setCcrName(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(3))) {
                cableRouteEntity.setFortificationIntensity(ShpAnalyticUtil.getStringVal(list.get(3).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(4))) {
                cableRouteEntity.setLineLength(ShpAnalyticUtil.getDoubleVal(list.get(4).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(5))) {
                cableRouteEntity.setCostLine(ShpAnalyticUtil.getDoubleVal(list.get(5).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(6))) {
                cableRouteEntity.setSite(ShpAnalyticUtil.getStringVal(list.get(6).toString(), "场地类别", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(7))) {
                cableRouteEntity.setLevel(ShpAnalyticUtil.getStringVal(list.get(7).toString(), "电压等级", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(8))) {
                cableRouteEntity.setLongitude(ShpAnalyticUtil.getStringVal(list.get(8).toString(), "经度", true));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(9))) {
                cableRouteEntity.setLatitude(ShpAnalyticUtil.getStringVal(list.get(9).toString(), "纬度", true));
            }
            if (list.size() > 10 && !PlatformObjectUtils.isEmpty(list.get(10))) {
                cableRouteEntity.setExtends1(list.get(10).toString());
            }
            if (list.size() > 11 && !PlatformObjectUtils.isEmpty(list.get(11))) {
                cableRouteEntity.setExtends2(list.get(11).toString());
            }
            if (list.size() > 12 && !PlatformObjectUtils.isEmpty(list.get(12))) {
                cableRouteEntity.setExtends3(list.get(12).toString());
            }
            if (list.size() > 13 && !PlatformObjectUtils.isEmpty(list.get(13))) {
                cableRouteEntity.setExtends4(list.get(13).toString());
            }
            if (list.size() > 14 && !PlatformObjectUtils.isEmpty(list.get(14))) {
                cableRouteEntity.setExtends5(list.get(14).toString());
            }
            if (list.size() > 15 && !PlatformObjectUtils.isEmpty(list.get(15))) {
                cableRouteEntity.setExtends6(list.get(15).toString());
            }
            if (list.size() > 16 && !PlatformObjectUtils.isEmpty(list.get(16))) {
                cableRouteEntity.setExtends7(list.get(16).toString());
            }
            if (list.size() > 17 && !PlatformObjectUtils.isEmpty(list.get(17))) {
                cableRouteEntity.setExtends8(list.get(17).toString());
            }
            if (list.size() > 18 && !PlatformObjectUtils.isEmpty(list.get(18))) {
                cableRouteEntity.setExtends9(list.get(18).toString());
            }
            if (list.size() > 19 && !PlatformObjectUtils.isEmpty(list.get(19))) {
                cableRouteEntity.setExtends10(list.get(19).toString());
            }
            cableRouteEntity.setVersionId(versionId);
            arrayList.add(cableRouteEntity);

            SmxMainTableEntity mainEntity = new SmxMainTableEntity();
            mainEntity.setId(id);
            mainEntity.setCreateUser(userId);
            mainEntity.setFortification(cableRouteEntity.getFortificationIntensity());
            mainEntity.setBusinessObject(FxfzConstants.CZT_SMX_DLXL);
            mainEntity.setVersionId(versionId);
            mainTableUtils.saveMainTable(mainEntity);
        }
        boolean res = false;
        int i = 0;
        for (SmxCableRouteEntity cableRouteEntity : arrayList) {
            cableRouteEntity.setSerialNumber(i);
            res = lifelineEngineeringRepository.saveCableRoute(cableRouteEntity);
            i += 1;
        }
        map.put("res", res);
        return map;
    }

    private Map<String, Object> uploadTvTowerShp(String fileIds, String versionId, String userId) throws ShpFieldException {
        Map<String, Object> map = new HashMap<>();
        shpUtils.uniformFileName(fileIds);
        ParsingResult shpData = shpUtils.getShpData(fileIds);
        //是否成功
        if (!shpData.getMessage().equals("ok")) {
            map.put("res", false);
            map.put("mess", shpData.getMessage());
        }
        List<List<Object>> shpList = shpData.getShpList();
        List<SmxTvTowerEntity> arrayList = new ArrayList<>();
        for (int i = 0; i < shpList.size(); i++) {
            String id = UUIDGenerator.getUUID();
            List<Object> list = shpList.get(i);
            if (list.size() < 19) {
                return null;
            }
            SmxTvTowerEntity tvTowerEntity = new SmxTvTowerEntity();
            tvTowerEntity.setDelFlag(YNEnum.N.toString());
            tvTowerEntity.setCttId(UUIDGenerator.getUUID());
            tvTowerEntity.setTableId(id);
            tvTowerEntity.setCreateUser(userId);
            tvTowerEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());

            if (!PlatformObjectUtils.isEmpty(list.get(0))) {
                tvTowerEntity.setGeom(list.get(0).toString());
            }
            if (!PlatformObjectUtils.isEmpty(list.get(1))) {
                tvTowerEntity.setObjectName(ShpAnalyticUtil.getStringVal(list.get(1).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(2))) {
                tvTowerEntity.setLocation(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(3))) {
                tvTowerEntity.setObjectLon(ShpAnalyticUtil.getStringVal(list.get(3).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(4))) {
                tvTowerEntity.setObjectLat(ShpAnalyticUtil.getStringVal(list.get(4).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(5))) {
                tvTowerEntity.setCttType(ShpAnalyticUtil.getStringVal(list.get(5).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(6))) {
                tvTowerEntity.setArea(ShpAnalyticUtil.getDoubleVal(list.get(6).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(7))) {
                tvTowerEntity.setYear(ShpAnalyticUtil.getIntegerVal(list.get(7).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(8))) {
                tvTowerEntity.setHeight(ShpAnalyticUtil.getDoubleVal(list.get(8).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(9))) {
                tvTowerEntity.setTowerElevation(ShpAnalyticUtil.getDoubleVal(list.get(9).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(10))) {
                tvTowerEntity.setBuldingSite(ShpAnalyticUtil.getStringVal(list.get(10).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(11))) {
                tvTowerEntity.setDesignIntensity(ShpAnalyticUtil.getIntegerVal(list.get(11).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(12))) {
                tvTowerEntity.setFieldType(ShpAnalyticUtil.getIntegerVal(list.get(12).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(13))) {
                tvTowerEntity.setStatus(ShpAnalyticUtil.getStringVal(list.get(13).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(14))) {
                tvTowerEntity.setReconstructionCost(ShpAnalyticUtil.getDoubleVal(list.get(14).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(15))) {
                tvTowerEntity.setAntennaElevation(ShpAnalyticUtil.getDoubleVal(list.get(15).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(16))) {
                tvTowerEntity.setPeriod(ShpAnalyticUtil.getDoubleVal(list.get(16).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(17))) {
                tvTowerEntity.setColmnType(ShpAnalyticUtil.getStringVal(list.get(17).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(18))) {
                tvTowerEntity.setBaseType(ShpAnalyticUtil.getStringVal(list.get(18).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(19))) {
                tvTowerEntity.setBaseStoryNum(ShpAnalyticUtil.getIntegerVal(list.get(19).toString(), "", false));
            }
            if (!PlatformObjectUtils.isEmpty(list.get(20))) {
                tvTowerEntity.setBaseHeight(ShpAnalyticUtil.getDoubleVal(list.get(20).toString(), "", false));
            }
            if (list.size() > 21 && !PlatformObjectUtils.isEmpty(list.get(21))) {
                tvTowerEntity.setExtends1(list.get(21).toString());
            }
            if (list.size() > 22 && !PlatformObjectUtils.isEmpty(list.get(22))) {
                tvTowerEntity.setExtends2(list.get(22).toString());
            }
            if (list.size() > 23 && !PlatformObjectUtils.isEmpty(list.get(23))) {
                tvTowerEntity.setExtends3(list.get(23).toString());
            }
            if (list.size() > 24 && !PlatformObjectUtils.isEmpty(list.get(24))) {
                tvTowerEntity.setExtends4(list.get(24).toString());
            }
            if (list.size() > 25 && !PlatformObjectUtils.isEmpty(list.get(25))) {
                tvTowerEntity.setExtends5(list.get(25).toString());
            }
            if (list.size() > 26 && !PlatformObjectUtils.isEmpty(list.get(26))) {
                tvTowerEntity.setExtends6(list.get(26).toString());
            }
            if (list.size() > 27 && !PlatformObjectUtils.isEmpty(list.get(27))) {
                tvTowerEntity.setExtends7(list.get(27).toString());
            }
            if (list.size() > 28 && !PlatformObjectUtils.isEmpty(list.get(28))) {
                tvTowerEntity.setExtends8(list.get(28).toString());
            }
            if (list.size() > 29 && !PlatformObjectUtils.isEmpty(list.get(29))) {
                tvTowerEntity.setExtends9(list.get(29).toString());
            }
            if (list.size() > 30 && !PlatformObjectUtils.isEmpty(list.get(30))) {
                tvTowerEntity.setExtends10(list.get(30).toString());
            }
            tvTowerEntity.setVersionId(versionId);
            arrayList.add(tvTowerEntity);

            SmxMainTableEntity mainEntity = new SmxMainTableEntity();
            mainEntity.setId(id);
            mainEntity.setCreateUser(userId);
            mainEntity.setFortification(tvTowerEntity.getDesignIntensity().toString());
            mainEntity.setBusinessObject(FxfzConstants.CZT_SMX_DST);
            mainEntity.setVersionId(versionId);
            mainTableUtils.saveMainTable(mainEntity);
        }

        boolean res = false;
        int i = 0;
        for (SmxTvTowerEntity tvTowerEntity : arrayList) {
            tvTowerEntity.setSerialNumber(i);
            res = lifelineEngineeringRepository.saveTvTower(tvTowerEntity);
            i += 1;
        }
        map.put("res", res);
        return map;
    }

    @Override
    public List<String> getVectorDataIdsByVersionId(String versionId, Integer type) {
        return lifelineEngineeringRepository.getVectorDataIdsByVersionId(versionId, type);
    }

    @Autowired
    private JcsjRestManager jcsjRestManager;

    @Override
    public HashMap<String, Object> getByRange(CircleParam param) {
        String geom;
        HashMap<String, Object> map = new HashMap<>();
        if (param.getViewingMethods() == 1) {
            geom = jcsjRestManager.getGeographicLocationByRange(param.getRange());
        } else {
            geom = param.getGeom();
        }
        param.getMap().forEach((k, v) -> {
            if (PlatformObjectUtils.isNotEmpty(v)) {
                ArrayList<Object> data = lifelineEngineeringRepository.getByTypeAndGeomAndVersionId(geom, Integer.parseInt(k), v, param.getViewingMethods());
                map.put(k, data);
            }
        });
        return map;
    }
}
